package com.xgp.mywordapp.controller;

import com.xgp.mywordapp.model.User;
import com.xgp.mywordapp.model.Words;
import com.xgp.mywordapp.service.UserService;
import com.xgp.mywordapp.service.WordService;
import com.xgp.mywordapp.tool.CheckWord;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import javax.websocket.Session;
import java.util.List;

@RestController
public class wordsController {

    @Autowired
    private WordService wordService;

    @RequestMapping("/addWordMethod")
    public String addWordMethod(
                @RequestParam("english") String english,
                @RequestParam("chinese") String chinese
            ) {

        //参数校验
        if(english.isEmpty() || chinese.isEmpty()) {
            return "提交的数据不完整";
        }

        if(CheckWord.checkCinese(english) == true || CheckWord.checkCinese(chinese) == false) {
            return "添加的单词不正确";
        }

        Words word = new Words();
        word.setWid(null);
        word.setEnglish(english);
        word.setChinese(chinese);
        word.setAllerror(0);
        word.setDatetime(String.valueOf(System.currentTimeMillis()));

        //调用添加单词的方法
        int flag = wordService.addWord(word);

        if(flag == 2) {
            return "该单词已经存在";
        }

        if(flag == 1) {
            //添加成功
            return "1";
        }
        //添加失败
        return "0";
    }
}
